package com.google.code.synchunit;

public class BlockDetector extends Thread {
	private Thread threadToDetect;
	boolean terminate;

	protected BlockDetector(Thread thread) {
		super("BlockDetector for " + thread.getName());
		threadToDetect = thread;
	}

	@Override
	public void run() {
		while (true) {
			if (terminate) {
				break;
			}
			if (threadToDetect.getState().equals(State.BLOCKED)) {
				synchronized (threadToDetect) {
					threadToDetect.notify();
				}
			}
			try {
				Thread.sleep(40);
			} catch (InterruptedException e) {
				throw new RuntimeException("BlockDetector for Thread "
						+ threadToDetect.getName() + " interrupted.", e);
			}
		}
	}

	protected void terminate() {
		terminate = true;
	}

}
